package com.inspector.app.ui.flight.mapsearch

import android.graphics.Color
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DiffUtil.ItemCallback
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.efb.module.ui.search.MapSearchFragment
import com.inspector.app.R
import com.inspector.app.databinding.MapsearchItemBinding
import com.inspector.app.di.util.inflate
import com.inspector.app.ui.flight.mapsearch.MapSearchAdapter.MapSearchVH
import com.inspector.app.ui.flight.mapsearch.poi.MapSearchPoi

/**
 * 搜索列表
 * @author pank 2023/4/4
 */
class MapSearchAdapter(
    private val mapSearchFragment: MapSearchFragment
) : ListAdapter<MapSearchPoi, MapSearchVH>(
    object : ItemCallback<MapSearchPoi>() {
        override fun areItemsTheSame(oldItem: MapSearchPoi, newItem: MapSearchPoi): Boolean {
            return oldItem == newItem
        }

        override fun areContentsTheSame(oldItem: MapSearchPoi, newItem: MapSearchPoi): Boolean {
            return oldItem == newItem && oldItem.colorSpan == newItem.colorSpan
        }

    }) {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MapSearchVH {
        return createMapSearchVH(parent, mapSearchFragment)
    }

    override fun onBindViewHolder(holder: MapSearchVH, position: Int) {
        holder.bind(getItem(position))
    }

    class MapSearchVH(
        private val binding: MapsearchItemBinding,
        private val mapSearchFragment: MapSearchFragment
    ) : RecyclerView.ViewHolder(binding.root) {
        
        fun bind(item: MapSearchPoi) {
            //[航线等图片颜色为透明色]
            /*if (item.itemType != MapLayerInstance.pointType) {
                binding.itemIVType.setColorFilter(Color.parseColor("#529fe5"))
            } else {
                binding.itemIVType.clearColorFilter()
            }*/
            //[colorSpan]
            if (item.searchNameWeight == Int.MAX_VALUE) {
                binding.itemTVTitle.setTextColor(Color.parseColor("#529fe5"))
            } else {
                binding.itemTVTitle.setTextColor(ContextCompat.getColor(binding.itemTVTitle.context, R.color.colorSettingsText))
            }
            binding.item = item
            binding.position = bindingAdapterPosition
            binding.lifecycleOwner = mapSearchFragment.viewLifecycleOwner
            binding.handler = mapSearchFragment
            binding.executePendingBindings()

            // 2024/1/18 [障碍物图标单独设置]
            /*if (item.itemType == MapLayerInstance.pointType && item.iconType == ConstantUtil.obstaclePointIconType) {
                binding.itemIVType.setImageResource(ConstantUtil.obstaclePointIcon)
            } else {
                binding.itemIVType.setImageResource(MapSearchInstance.getDrawIRs(ConstantUtil.POINT_ICON, item.iconType))
            }*/
        }

    }

    private fun createMapSearchVH(
        parent: ViewGroup,
        mapSearchFragment: MapSearchFragment
    ): MapSearchVH {
        val bind = MapsearchItemBinding.bind(parent.inflate(R.layout.mapsearch_item))
        return MapSearchVH(bind, mapSearchFragment)
    }

}
